package com.wyh.blog.controller;

import com.wyh.blog.aspect.BlogInfoDetailException;
import com.wyh.blog.aspect.BlogInfoException;
import com.wyh.blog.model.vo.request.AddBlogRequest;
import com.wyh.blog.model.vo.request.UpdateRequest;
import com.wyh.blog.model.vo.response.BlogDetailVo;
import com.wyh.blog.model.vo.response.BlogInfoVo;
import com.wyh.blog.service.intf.BlogInfoService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/blog")
@Slf4j
public class BlogInfoController {
    @Resource(name = "blogInfoServiceImpl")
    private BlogInfoService blogInfoService;

    @RequestMapping("/getList")
    public List<BlogInfoVo> getList(){
        log.info("获取博客列表");
        try {
            List<BlogInfoVo> blogInfoVos = blogInfoService.getList();
            return blogInfoVos;
        }catch (Exception e){
            log.error("获取博客列表失败，e: ",e);
            throw new BlogInfoException("获取博客列表失败");
        }
    }

    @RequestMapping("/getBlogDetail")
    public BlogDetailVo getBlogDetail(@NotNull(message = "id不能为空") Integer id){
        log.info("查看全文，id:{}",id);
        try {
            return blogInfoService.getBlogDetail(id);
        }catch (Exception e){
            log.error("查看全文异常，id：{}", id);
            throw new BlogInfoDetailException("查看全文失败");
        }
    }

    @RequestMapping("/add")
    public boolean add(@RequestBody @Validated AddBlogRequest addBlogRequest){
        log.info("发布博客，userId:{} , title:{}", addBlogRequest.getUserId(), addBlogRequest.getTitle());
        return blogInfoService.add(addBlogRequest);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody @Validated UpdateRequest request){
        log.info("博客更新，id:{}", request);
        return blogInfoService.update(request);
    }

    @RequestMapping("/delete")
    public boolean delete(Integer id){
        log.info("删除博客， id: {}", id);
        return blogInfoService.delete(id);
    }
}
